package com.os.start.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.os.start.mapper.SUserMapper;
import com.os.start.mapper.SUserMapperCustom;
import com.os.start.pojo.SUser;
import com.os.start.service.UserService;
import com.os.utils.JqGridResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @Author Matt
 * @Date 2022-01-06
 * @Desc:
 * @Version 1.0
 */

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private SUserMapper sUserMapper;

    @Autowired
    private SUserMapperCustom sUserMapperCustom;

    @Override
    public void saveUser(SUser user) {
        sUserMapper.insert(user);
    }

    @Override
    public void updateUser(SUser user) {
        sUserMapper.updateByPrimaryKey(user);
    }

    @Override
    public void deleteUser(String userId) {
        sUserMapper.deleteByPrimaryKey(userId);
    }

    @Override
    public SUser queryUserById(String userId) {
        return sUserMapper.selectByPrimaryKey(userId);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<SUser> queryUserList(SUser user) throws InterruptedException {

        java.lang.Thread thread = new java.lang.Thread();
        thread.sleep(5001);

        Example example = new Example(SUser.class);
        Example.Criteria criteria = example.createCriteria();

        if(!StringUtils.isEmptyOrWhitespace(user.getName())){
            criteria.andLike("name", "%" + user.getName() + "%");
        }

        if(!StringUtils.isEmptyOrWhitespace(user.getSex())){
            criteria.andLike("sex", "%" + 1 + "%");
        }

        List<SUser> userList = sUserMapper.selectByExample(example);

        return userList;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<SUser> queryUserListPaged(SUser user, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);

        Example example = new Example(SUser.class);
        Example.Criteria criteria = example.createCriteria();

        if(!StringUtils.isEmptyOrWhitespace(user.getName())){
            criteria.andLike("name", "%" + user.getName() + "%");
        }

        if(!StringUtils.isEmptyOrWhitespace(user.getSex())){
            criteria.andLike("sex", "%" + 1 + "%");
        }

        List<SUser> userList = sUserMapper.selectByExample(example);

        return userList;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public JqGridResult queryUserListPagedJqGrid(SUser user, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);

        Example example = new Example(SUser.class);
        Example.Criteria criteria = example.createCriteria();

        if(!StringUtils.isEmptyOrWhitespace(user.getName())){
            criteria.andLike("name", "%" + user.getName() + "%");
        }

        if(!StringUtils.isEmptyOrWhitespace(user.getSex())){
            criteria.andLike("sex", "%" + 1 + "%");
        }

        List<SUser> userList = sUserMapper.selectByExample(example);

        PageInfo<SUser> pageList = new PageInfo<SUser>(userList);

        JqGridResult grid = new JqGridResult();
        grid.setTotal(pageList.getPages());
        grid.setRows(userList);
        grid.setPage(page);
        grid.setRecords(pageList.getTotal());

        return grid;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<SUser> queryUserListCustom(String id) {
        return sUserMapperCustom.queryUserInfoById(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void saveUserTransactional(SUser user) {
        sUserMapper.insert(user);

        int i = 1/0;

        user.setSex("南方");
        sUserMapper.updateByPrimaryKey(user);
    }


}
